import Vue from "vue";
import ViewUI from "view-design";
import '../styles/tailwind.css';
import '../styles/index.scss';
import iViewPro from 'public/3thComponents/iview-pro/iview-pro.min.js';
import 'public/3thComponents/iview-pro/iview-pro.css';
import 'public/styles/index.less';
import 'view-design/dist/styles/iview.css';
import 'public/3thComponents/iviewAdmin';
import formCreate from '@form-create/iview4'

Vue.use(iViewPro);
import TableImportExport from 'public/3thComponents/iview-pro/table-import-export.min';
Vue.use(TableImportExport);
// import iSidebar from 'public/3thComponents/iview-sidebar/sidebar.min';
// import 'public/3thComponents/iview-sidebar/sidebar.css'
// Vue.use(iSidebar);
import App from '../App'
import tools from 'public/tools/index';
Vue.prototype.tools=tools;
import store from '@/store';
import globalMixin from 'public/mixins/globalMixin';
import {getRouter, handleChildren} from "../router/index";
import {getMenu,getDynaMenu,getMenuSider} from "../menu/";
import 'public/components';
Vue.mixin(globalMixin);
Vue.use(ViewUI);
Vue.use(formCreate);
import plugins from 'public/plugins';
Vue.use(plugins);
import initialI18n from 'public/i18n';
import echarts from "echarts";
Vue.prototype.echarts=echarts;
import dataV from '@jiaminghi/data-view'
Vue.use(dataV)
// import '@/mock'
export async function initial(fn) {
    let setting=require('../setting').default;
    store.commit('initial',{tools,setting});
    let i18n=initialI18n(setting.i18n.default);
    let api=require('public/service/scenario').api;
    store.commit('setApi',api);
    Vue.prototype.$api=api;
    let modelObject=require('public/model/starModel').default;
    Vue.prototype.$modelObject=modelObject;
    // let abc = 1>0??0;
    // let models = await api.Model.getAllModel();
    // store.dispatch('admin/model/setAll',models)

    // old
    // let models = await api.Model.getAllModel();
    // store.dispatch('admin/model/setAll',models)
    // store.commit('admin/layout/setCurrentAccount',api);
    // let menuList = await getDynaMenu(api)
    // let menuSider = getMenuSider(menuList);
    // let menu=getMenu(menuSider);
    // store.commit('admin/layout/setMenu',menu);
    // let router=getRouter(setting,store);

    let loginFlag = await loginInfo(store, setting, api);
    if (loginFlag) {
        let router = getRouter(setting, store);
        /**eventBus*/
        let eventBus = new Vue();
        window.EventBus = eventBus;
        Vue.prototype.$EventBus = eventBus;
        window.store = store;
        /**return*/
        return {router, store, i18n, App}
    }
}

async function loginInfo(store,setting,api) {
    let flag = true;
    if(!setting.needSSo || isAndroid()){
        await initMenu(store, setting, api);
        return flag;
    }
    let code = getQueryString('code') || ''
    if(code){
        tools.cookies.remove('token');
        tools.cookies.remove('uuid');
    }
    const token = tools.cookies.get('token');
    if (!token) {
        if (code) {
            console.log('获取登录信息')
            //如果有code则调用接口换取token
            let res =  await api.BaseSso.accessToken(code);
            let loginUser = res.data;
            if (loginUser) {
                //请求结束获取到数据
                tools.cookies.set('token', res.token)
                tools.cookies.set('uuid', loginUser.id)
                await initMenu(store, setting, api)
            }
        } else {
            flag = false;
            //如果没有登录切url参数上没有code参数则302到supOS换取认证。SUPOS_URL本地调试时设置为沙箱地址，安装到supOS时设置为空
            window.location.href =store.state.setting.apiBaseURL+"star4.0/sso/authorize?redirect_url=" + encodeURIComponent(window.location.href)
        }
    }else{
        await initMenu(store, setting, api)
    }
    return flag;
}

function isAndroid() {
    // 返回由客户机发送服务器的user-agent 头部的值
    const device = navigator.userAgent
    //做判断 通过返回的数据来筛选查找关键信息
    if (device.indexOf('Android') > -1 && window.innerWidth <= 720) {
        // 手机
        return true;
    } else {
        return false;
    }
}

async function initMenu(store,setting,api) {
    store.commit('admin/layout/setCurrentAccount',api);
    let menuList = await getDynaMenu(api)
    let menuSider = getMenuSider(menuList);
    let menu=getMenu(menuSider);
    store.commit('admin/layout/setMenu',menu);
}

/*
* 获取浏览器参数
* key：参数名
*/
function getQueryString(key) {
    let reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)', 'i');
    let r = window.location.search.substring(1).match(reg);
    if (r != null) {
        return decodeURIComponent(r[2]);
    }
    return null;
}

